/**
 * 
 */
package com.sys.object.common.page;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.springframework.util.StringUtils;

import com.sys.console.business.SysModuleService;
import com.sys.console.pojo.SysModuleMeunDto;
import com.sys.console.pojo.Sys_Module;
import com.sys.object.common.context.ServiceInitContainer;

/**
 * @author zxp
 *
 */
public class MenuTag extends TagSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public int doStartTag() throws JspException {
		return EVAL_PAGE;
	}
	
	public int doEndTag() throws JspTagException {
		JspWriter out = this.pageContext.getOut();
		try {
			out.print(getMenuTree().toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}
	public StringBuffer getMenuTree(){
		StringBuffer buf = new StringBuffer();
		SysModuleService sysModuleService = ServiceInitContainer.getBean("sysModuleService");
		Sys_Module module = new Sys_Module();
		try {
			ArrayList<SysModuleMeunDto> list = sysModuleService.selectUserModuleByList(module);
			if(list!=null && list.size()>0){
				for(SysModuleMeunDto item:list){
					if(item.getPid() == 0){
						if(StringUtils.hasText(item.getIcon())){
							buf.append("<li><a href=\"javascript:void(0)\"><i class=\"fa "+item.getIcon()+"\"></i> <span class=\"nav-label\">"+item.getName()+"</span><span class=\"fa arrow\"></span></a>");
						}else{
							buf.append("<li><a href=\"javascript:void(0)\"><i class=\"fa fa-gear\"></i> <span class=\"nav-label\">"+item.getName()+"</span><span class=\"fa arrow\"></span></a>");
						}
						buf.append("<ul class=\"nav nav-second-level\">");
						if(item.getChildList()!=null && item.getChildList().size()>0){
							for(SysModuleMeunDto citem:item.getChildList()){
								if(StringUtils.hasText(citem.getIcon())){
									buf.append("<li><a href="+citem.getUrl()+" class=\"J_menuItem\"></i> <i class=\"fa "+citem.getIcon()+" \"></i><span class=\"nav-label\">"+citem.getName()+"</span></a></li>");
								}else{
									buf.append("<li><a href="+citem.getUrl()+" class=\"J_menuItem\"></i> <span class=\"nav-label\">"+citem.getName()+"</span></a></li>");
								}
							}
						}
						buf.append("</ul><li>");
					}
				}
				return buf;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return buf;
	}
}
